package com.example.duanjunjie.learn.rxjava;

import android.util.Log;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;

/**
 * Created by duanjunjie on 2018/2/27.
 */

public class RxJavaTest {
    private int temp = 1;

    public int testRxjava5() {

        Observable.just(1, 2, 3, 4).map(new Func1<Integer, Integer>() {
            @Override
            public Integer call(Integer integer) {
                return integer + 1;
            }
        }).filter(new Func1<Integer, Boolean>() {
            @Override
            public Boolean call(Integer integer) {
                return integer == 3;
            }
        }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                temp = integer;
            }
        });

        return temp;
    }

    void testRxjava4() {
        PublishSubject<Integer> publishSubject = PublishSubject.create();
        publishSubject.map(new Func1<Integer, String>() {
            @Override
            public String call(Integer i) {
                return "call" + i;
            }
        }).subscribe(new Subscriber<String>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String s) {
                System.out.println("onNext" + s);
            }
        });

        publishSubject.onNext(2);
    }


    void testRxjava3() {

        Subscription subscription = Observable.create(new Observable.OnSubscribe<Object>() {

            @Override
            public void call(Subscriber<? super Object> subscriber) {

            }
        }).flatMap(new Func1<Object, Observable<?>>() {

            @Override
            public Observable<?> call(Object o) {
                return null;
            }
        }).subscribe(new Subscriber<Object>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object o) {

            }
        });
    }


    //lift
    void testRxjava2() {
        Observable.create(new Observable.OnSubscribe<Integer>() {

            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                for (int i = 0; i < 5; i++) {
                    subscriber.onNext(i);
                }
                subscriber.onCompleted();
            }
        }).lift(new Observable.Operator<String, Integer>() {

            @Override
            public Subscriber<? super Integer> call(final Subscriber<? super String> subscriber) {
                return new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        subscriber.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        subscriber.onError(e);
                    }

                    @Override
                    public void onNext(Integer integer) {
                        subscriber.onNext("haha" + integer);
                    }
                };
            }
        }).subscribe(new Subscriber<String>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onCompleted() {
                System.out.println("completed");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String o) {
                System.out.println(o);
            }
        });
    }

    //map
    void testRxjava() {
        Observable.just('a', 'b', 'c', 'd')
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<Character, Integer>() {
                    @Override
                    public Integer call(Character c) {
                        return Integer.valueOf(c);
                    }
                }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer o) {
                Log.i("djj", o.toString());
            }
        });
    }
}
